clear
set more off

* EXTRACT LEVEL 02 DATA 

local topdir "\nss_64_10"
local dirclean "cd "`topdir'\data_output" "
local dirdict "cd "`topdir'\dict" "
local dirraw "cd "`topdir'\raw_data" "
local dirdo "cd "`topdir'\do_files" "

foreach x  in 1 2 3 4 5 6 7 8 {
infile using "`topdir'\dict\level02.txt", using ("`topdir'\raw_data\AH`x'C102.txt")
keep if level=="02"
save "`topdir'\data_output\data`x'_01.dta", replace
clear
}


clear
use "`topdir'\data_output\data1_01.dta"
foreach x  in  2 3 4 5 6 7 8 {
append using "`topdir'\data_output\data`x'_01.dta"
erase "`topdir'\data_output\data`x'_01.dta"
}
gen common_id =  lot_fsu_number+ hamlet_group_sub_block_no+ second_stage_stratum+ hh_id
gen state = substr(state_region,1,2)
gen hh_type_r = hh_type if sector == "1"
gen hh_type_u = hh_type if sector == "2"
order common_id centre_code_round_shift - hh_type hh_type_r hh_type_u
destring centre_code_round_shift - state, replace

do "`topdir'\do_files\label_define.do"
label values sample sample
label values sector sector
label values hh_type_r hh_type_r
label values hh_type_u hh_type_u
label values religion religion
label values social_group social_group
label values land_possessed_code land_possessed_code
label values loc_last_usual_pl_residence loc_last_usual_pl_residence
label values pattern_migration pattern_migration
label values reason_migration reason_migration
label values use_remittances_first_code use_remittances
label values use_remittances_second_code use_remittances
label values use_remittances_third_code use_remittances
label values state state_64
sort common_id
save "`topdir'\data_output\level02.dta", replace
erase "`topdir'\data_output\data1_01.dta"
